from dream_class.ConnDb import OpenDb
from bs4 import BeautifulSoup, Comment
import time
# import requests


def insert_area_list(html_cont: str)-> str:
    """
    在源码中查找特定内容: div的id=areaSearch
    获取大区及城市列表
    解析后，添加到表：nisshinkyo_area_list，大区parent_id=0,城市parent_id等于大区id
    插入数据时，使用 OpenMySqlDb.insert_data 类中函数
    每插入一个大区，输出一行结果
    每插入一个城市，输出一行结果
    返回文字说明：所有完成
    """
    try:
        soup = BeautifulSoup(html_cont, 'html.parser')
        # 查找id为areaSearch的表格,就是本页需要的城市列表
        res_cont = soup.find(attrs={'id':'areaSearch'})
        if res_cont:
            db = OpenDb()
            # BeautifulSoup的Comment 清除注释
            for comment in soup.findAll(text=lambda text: isinstance(text, Comment)):
                comment.extract()
            # 获取当前7个大区
            curr_tr = res_cont.findAll('tr')
            # 循环7个大区
            for tr_cont in curr_tr:
                # 获取当前大区的th为标题
                th_cont = tr_cont.find('th')
                area_name = th_cont.text
                area_link = th_cont.find('a')['href']
                # 插入大区为数据库一级栏目
                ins_area_dict = {}
                ins_area_dict['area_name'] = area_name
                ins_area_dict['area_link'] = 'https://www.nisshinkyo.org/search/'+area_link
                ins_area_dict['parent_id'] = 0
                re_add_area = db.insert_data(db.PJ_area_city, ins_area_dict)
                # 如果插入大区成功，再去处理大区下的城市
                if re_add_area:
                    print('大区', area_name, "\t",'id: ' + str(re_add_area))
                    # 获取当前td为大区下城市列表的主体
                    td_cont = tr_cont.find('td')
                    # 获取城市列表
                    curr_all_a = td_cont.find_all('a')
                    for one_a in curr_all_a:
                        # 城市名及链接
                        city_name = one_a.text
                        city_link = one_a['href']
                        # 插入城市列表为二级栏目
                        incity_dict = {}
                        incity_dict['area_name'] = city_name
                        incity_dict['area_link'] = 'https://www.nisshinkyo.org/search/'+city_link
                        incity_dict['parent_id'] = re_add_area
                        res_add_city = db.insert_data(db.PJ_area_city, incity_dict)
                        print("\t",'->城市', city_name, "\t",'id: ' + str(res_add_city))
                        time.sleep(0.3)
                        
                else:
                    print('大区', area_name, "\t",'插入操作未返回状态')
                    exit()
            
            # 所有循环完成后返回
            back_arr = {}
            back_arr['back_code'] = 0
            back_arr['back_msg'] = "大区 及 城市 所有存入完成"
            # 关闭数据库连接
            db.close_connection()
            return back_arr
        
        else:
            back_arr = {}
            back_arr['back_code'] = 1001
            back_arr['back_msg'] = "未找到内容"
            return back_arr
        
    except ValueError as e:
        return e

